package com.fps.web.controller.basemenu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fps.web.model.Basemenu;
import com.fps.web.service.BasemenuService;
import com.fpswork.core.model.ExcelEntity;
import com.fpswork.core.model.ExcelShell;
import com.fpswork.core.model.PageEntity;
import com.fpswork.util.ExcelUtil;
import com.fpswork.util.StringUtil;
import com.fps.web.controller.BaseController;

/**
 * 菜单页面
 *
 */
@Controller
@RequestMapping("/basemenu")
public class BasemenuController extends BaseController{
    
    @Autowired
    BasemenuService basemenuService;
    
    
   
    /**
	 * 加载List数据
	 */
    @RequestMapping(value = "/loadBasemenuList", method = RequestMethod.GET)
    public @ResponseBody PageEntity loadBasemenuList(PageEntity pageEntity,String menuname) throws Exception{
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("menuname", menuname);
    	pageEntity.setSearch(map);
    	basemenuService.queryBasemenuList(pageEntity);
        return pageEntity;
    }
    
    /**
   	 * 菜单管理
   	 */
    @RequestMapping("/basemenuManagement")
    public ModelAndView basemenuManagement(String id,String pagetype) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();   
    	Basemenu basemenu= new Basemenu();
    	if(!"add".equals(pagetype)){
    		basemenu = basemenuService.queryBasemenuByID(id);	
    	}
    	modelAndView.addObject("basemenu",basemenu);
    	modelAndView.addObject("pagetype", pagetype);
		modelAndView.setViewName("web/basemenu/basemenumanagement");      
        return modelAndView;
    }
    
    /**
   	 * 菜单管理新增表单
   	 */
    @RequestMapping(value = "/basemenuAddSubmit", method = RequestMethod.POST)
    public @ResponseBody Map<String, String> basemenuAddSubmit(@ModelAttribute("Basemenu")Basemenu basemenu) throws Exception{
    	basemenuService.addBasemenu(basemenu);
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("flag", "true");
        return map;
    }
    
    /**
   	 * 菜单管理修改表单
   	 */
    @RequestMapping(value = "/basemenuUpdateSubmit", method = RequestMethod.POST)
    public @ResponseBody Basemenu basemenuUpdateSubmit(@ModelAttribute("Basemenu")Basemenu basemenu) throws Exception{
    	basemenuService.updateBasemenu(basemenu);
        return basemenu;
    }
    
    /**
   	 * 菜单删除
   	 */
    @RequestMapping(value = "/deleteBasemenu", method = RequestMethod.POST)
    public @ResponseBody Map<String, String> deleteBasemenu(@ModelAttribute("Basemenu")Basemenu basemenu) throws Exception{
    	basemenuService.deleteBasemenu(basemenu);
    	Map<String, String> map = new HashMap<String, String>();
		map.put("flag", "true");
        return map;
    }
	/**
   	 * DownExcel
   	 * 下载菜单excel
   	 */
    @RequestMapping(value = "/DownExeclBasemenu", method = RequestMethod.GET)
    public @ResponseBody Map<String, String> DownExeclBasemenu(HttpServletResponse res,HttpServletRequest req,String exceldata,String menuname) throws Exception{
       	Map<String, String> map = new HashMap<String, String>();
       	ExcelEntity excelEntity = new ExcelEntity();
       	//存放对应的表头名称和对应的数据库字段
    	Map<String, Object> titlemap = new LinkedHashMap<String, Object>();
    	//下载文件名
    	String fileName=".xls";
       	if(exceldata.contains("||") ){// 以"||"分割的字符串
	    	  List<String> arg=  StringUtil.splitString(exceldata, "||");
	    	  for(int i=0;i<arg.size();i++){
	    		  if(i==0){
	    			  fileName=arg.get(0)+fileName; 
	    		  }else{
	    			  String[] arg1=   arg.get(i).split(":"); 
	    			  titlemap.put(arg1[0], arg1[1]);
	    		  }
	    	  }
	    }
       
    	excelEntity.setFilename(fileName);
    	excelEntity.setLinename(titlemap);
    	//直接查询结果集的语句
    	String sql="select * from basemenu where 1=1";
		if(StringUtil.isNotEmpty(menuname)){
			sql+=" and basemenu.menuname='"+menuname+"' ";
		}
    	excelEntity.setSql(sql);
    	this.DownExecl(res, req, excelEntity);
    	map.put("flag", "true");
        return map;
    }

}
